package com.test.selenium.util;

import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.RandomStringUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.test.selenium.AirSmokeTest;
import com.test.selenium.data.InputTestData;
import com.test.selenium.data.MovementData;
import com.test.selenium.data.OutputSheet;
import com.test.selenium.data.Results;
import com.test.selenium.data.UTiLogin;

public class SmokeTestMovement {
	private boolean debugMode = false;
	public final double epsilon = 1E-10;
	
	private WebDriver driver;
	private String movementUrl;
	private SmokeTestTimeStamp timeStamp;
	private WebDriverWait wait;
	private MovementData movementData;
	private OutputSheet resultSheet;
	
	private String consolidationNum;
	private String carrierMasterBillNum;
	private String movementStatus;
	
	private String  potimestamp;
	
	public SmokeTestMovement(WebDriver driver, SmokeTestTimeStamp timeStamp, MovementData movementData, OutputSheet resultSheet) {
		this.driver = driver;
		this.timeStamp = timeStamp;
		this.movementData = movementData;
		this.resultSheet = resultSheet;
		movementUrl = UTiLogin.url + "#!movement";
		wait = new WebDriverWait(driver, 60);
		this.consolidationNum = "";
		this.carrierMasterBillNum = "";
		this.movementStatus = "";
	}
@SuppressWarnings("finally")
public void createMovement() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
	        //driver.findElement(By.id("movementHref")).click();
	        driver.get(movementUrl);
	        Thread.sleep(4000);
	       
	        new Select (driver.findElement(By.id("mode_code"))).selectByVisibleText(movementData.getModeCode());
	        driver.findElement(By.id("carrier_code_id")).sendKeys(movementData.getCarrierCode());
	        WebElement element5 = driver.findElement(By.id("carrier_code_id"));
	        element5.sendKeys(Keys.TAB);
	        Thread.sleep(1000);
	        new Select (driver.findElement(By.id("movement_type_code"))).selectByVisibleText(movementData.getMovementTypeCode());
	        Thread.sleep(1000);
	        //new Select (driver.findElement(By.id("distance_uom_code"))).selectByVisibleText(movementData.getUOM());
	        new Select (driver.findElement(By.id("distance_uom_code"))).selectByVisibleText("IMPERIAL");
	        driver.findElement(By.id("printing_currency_code")).sendKeys(movementData.getPrintingCurrency());
	        new Select (driver.findElement(By.id("payment_term_code"))).selectByVisibleText(movementData.getPaymenttermCode());
	        //Movement Parties
	        driver.findElement(By.name("primary_shipper")).sendKeys(movementData.getShipper());;
	        driver.findElement(By.name("primary_consignee")).sendKeys(movementData.getConsignee());;
	        driver.findElement(By.id("origin_site_code")).sendKeys(movementData.getOriginSiteCode());
	        driver.findElement(By.id("origin_port_code")).sendKeys(movementData.getOriginPortCode());
	        driver.findElement(By.id("destination_site_code")).sendKeys(movementData.getDestinationSiteCode());
	        driver.findElement(By.id("destination_port_code")).sendKeys(movementData.getDestinationPortCode());
	        driver.findElement(By.id("departure_date")).sendKeys(timeStamp.getDepartureDate());
	       // Adding Itinerary Leg to Movement         
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("movement_itinerary_leg_grid"))).click();
	        driver.findElement(By.id("movement_leg_button_area")).click();
	        driver.findElement(By.id("movement_leg_tableAdd")).click();
//	        driver.findElement(By.id("itinerary_leg_edit_dialog")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("vehicle_number"))).sendKeys(movementData.getFlightNumber());
	        driver.findElement(By.name("estimated_departure_local_datetime")).sendKeys(timeStamp.getDepartureDate());
	        driver.findElement(By.name("estimated_departure_local_time")).sendKeys(timeStamp.getDepartureTime());
	        driver.findElement(By.name("estimated_arrival_local_datetime")).sendKeys(timeStamp.getArrivalDate());
	        driver.findElement(By.name("estimated_arrival_local_time")).sendKeys(timeStamp.getArrivalTime());
	        driver.findElement(By.id("uti-add-row-save")).click();
	        Thread.sleep(2000);
	     //Generate Unique CarrierMasterBillNumber
	        carrierMasterBillNum = "MOV" + RandomStringUtils.randomNumeric(8);
	        driver.findElement(By.id("carrier_master_bill_number")).sendKeys(carrierMasterBillNum);
	        WebElement element13 = driver.findElement(By.id("carrier_master_bill_number"));
	        element13.sendKeys(Keys.TAB);
	        Thread.sleep(2000);
	        // Noted that the "YES" button to confirm movement carrier master bill number elements are duplicated
	        // Hence finding single element will not work.
	        List<WebElement> yesButtons = driver.findElements(By.id("uti-movement-yes"));
	        for (WebElement yesButton: yesButtons) {
	        	if (yesButton.isDisplayed()) {
	        		yesButton.click();
	        		break;
	        	}
	        }
	        Thread.sleep(4000);
	     //Save Movement
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[4]/button[3]"))).click();
	        long timeBeforeSave = System.currentTimeMillis();
	        // Wait until success icon indicating movement created successfully
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Movement created successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
	        
	     //print movement CarrierMasterBill and Consolidation Number
	        consolidationNum = driver.findElement(By.id("consolidation_number_text")).getAttribute("Value");
	        System.out.println("ConsolidationNumber = "+ consolidationNum);
	        String carrier_master_bill_number = driver.findElement(By.id("carrier_master_bill_number")).getAttribute("Value");
	        
	     // Verify Movement status as "SETUP"
	        movementStatus= driver.findElement(By.id("movement_status")).getText();
	        
	        if (debugMode) {
	        	System.out.println("carrier_master_bill_number = "+ carrier_master_bill_number);
	        	System.out.println("Movement status = "+ movementStatus);
	        }
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	String timeTakenString = null;
    		if (testTimeTakenSec < epsilon) {
    			timeTakenString = "-";
    		}
    		else {
    			timeTakenString = String.valueOf(testTimeTakenSec);
    		}
        	String testValue= "Consolidation number = "+ consolidationNum + "\nMovement status = "+ movementStatus;
        	if (!movementStatus.equalsIgnoreCase("SETUP")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: SETUP";
        	}
        	else{
        		isPass = "PASS";
        	}
        	try {
        		//Print EXCEL report
        	resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Create movement", isPass, timeTakenString, testValue);
 		    }
        	catch (final IOException e) {
				e.printStackTrace();
			}	  
       
        	//Print HTML report
			AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Create movement", isPass, testValue, timeTakenString));
        }
	}
	
	public void updateMovement() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Update Movement  
	        Thread.sleep(5000);
	        new Select (driver.findElement(By.id("distance_uom_code"))).selectByVisibleText(movementData.getUOM());
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[4]/button[3]"))).click();
	        // Wait until movement update successfully
	        long timeBeforeSave = System.currentTimeMillis();
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Movement Updated Successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
	        
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
        finally {
        	String timeTakenString = null;
    		if (testTimeTakenSec < epsilon) {
    			timeTakenString = "-";
    		}
    		else {
    			timeTakenString = String.valueOf(testTimeTakenSec);
    		}
        	String testValue= "Consolidation number = "+ consolidationNum + "\nMovement status = "+ movementStatus;
        	if (!movementStatus.equalsIgnoreCase("SETUP")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: SETUP";
        	}
        	else{
        		isPass = "PASS";
        	}
        	try {
        		//Print EXCEL report
        		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Update Movement", isPass, timeTakenString, testValue);
 		    }
       	catch (final IOException e) {
				e.printStackTrace();
			}	  
       
        	//Print HTML report
		AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Update movement", isPass, testValue, timeTakenString));
        }
	}
		
	
	public void addCharges() {
		String isPass= "PASS";
		double testTimeTakenSec = 0;
		
		try {
			Thread.sleep(4000);
			driver.findElement(By.className("charges")).click();
	        Thread.sleep(5000);
	        driver.findElement(By.id("suggested_vendors")).sendKeys("other_vendor");
	        WebElement element14_1 = driver.findElement(By.id("suggested_vendors"));
	        element14_1.sendKeys(Keys.TAB);
	        driver.findElement(By.id("vendor_account_number")).sendKeys("110939842");
	        WebElement element14_2 = driver.findElement(By.id("vendor_account_number"));
	        element14_2.sendKeys(Keys.TAB);        
	        Thread.sleep(3000);
	        driver.findElement(By.id("charge_code")).sendKeys(movementData.getChargeCode());
	        WebElement element14 = driver.findElement(By.id("charge_code"));
	        element14.sendKeys(Keys.TAB);
	        Thread.sleep(2000);
	        driver.findElement(By.id("movement_billing_charges")).click();
	        driver.findElement(By.id("movement_billing_charges_sell")).click();
	        driver.findElement(By.id("movement_cost_form")).click();
	        driver.findElement(By.className("fields")).click();
	        driver.findElement(By.className("columns")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[4]/div/div/div[2]/form/div[2]/div/ul/li/select"))).click();
	        driver.findElement(By.id("basis")).sendKeys(movementData.getBasis());
	        WebElement element16 = driver.findElement(By.id("basis"));
	        element16.sendKeys(Keys.TAB);
	        Thread.sleep(3000);
	        driver.findElement(By.id("costrate")).click();
	        driver.findElement(By.id("costrate")). sendKeys(movementData.getCostRate());
	        driver.findElement(By.id("printrate")). sendKeys(movementData.getPrintRate());
	        Thread.sleep(3000);
	        driver.findElement(By.className("buttons")).click();
	        driver.findElement(By.id("costCalculate")).click();
	        driver.findElement(By.id("costAdd")).click();
	        Thread.sleep(3000);
	        driver.findElement(By.xpath("/html/body/div/div[4]/button[3]")).click();
	     // Wait until movement charges added successfully  
	        long timeBeforeSave = System.currentTimeMillis();
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Transaction Charges updated Successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[2]/ul[3]/li/a"))).click();
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Add movement charges", isPass, timeTakenString);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Add Charges", isPass, "N/A", timeTakenString));
		}
	}
	
	public void addCharges2() {
		String isPass= "PASS";
		double testTimeTakenSec = 0;
		
		try {
			Thread.sleep(4000);
			driver.findElement(By.className("charges")).click();
	        Thread.sleep(5000);
//	        driver.findElement(By.id("suggested_vendors")).sendKeys("other_vendor");
//	        //WebElement element14_1 = driver.findElement(By.id("suggested_vendors"));
//	        driver.findElement(By.id("suggested_vendors")).sendKeys(Keys.TAB);
//	        driver.findElement(By.id("vendor_account_number")).sendKeys("110939842");
//	        //WebElement element14_2 = driver.findElement(By.id("vendor_account_number"));
//	        driver.findElement(By.id("vendor_account_number")).sendKeys(Keys.TAB);        
//	        Thread.sleep(3000);
	        driver.findElement(By.id("charge_code")).sendKeys(movementData.getChargeCode());
	        WebElement element14 = driver.findElement(By.id("charge_code"));
	        element14.sendKeys(Keys.TAB);
	        Thread.sleep(2000);
	        driver.findElement(By.id("movement_billing_charges")).click();
	        driver.findElement(By.id("movement_billing_charges_sell")).click();
	        driver.findElement(By.id("movement_cost_form")).click();
	        driver.findElement(By.className("fields")).click();
	        driver.findElement(By.className("columns")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[4]/div/div/div[2]/form/div[2]/div/ul/li/select"))).click();
	        driver.findElement(By.id("basis")).sendKeys(movementData.getBasis());
	        WebElement element16 = driver.findElement(By.id("basis"));
	        element16.sendKeys(Keys.TAB);
	        Thread.sleep(3000);
	        driver.findElement(By.id("costrate")).click();
	        driver.findElement(By.id("costrate")). sendKeys(movementData.getCostRate());
	        driver.findElement(By.id("printrate")). sendKeys(movementData.getPrintRate());
	        Thread.sleep(3000);
	        driver.findElement(By.className("buttons")).click();
	        driver.findElement(By.id("costCalculate")).click();
	        driver.findElement(By.id("costAdd")).click();
	        Thread.sleep(3000);
	        driver.findElement(By.xpath("/html/body/div/div[4]/button[3]")).click();
	     // Wait until movement charges added successfully  
	        long timeBeforeSave = System.currentTimeMillis();
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Transaction Charges updated Successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[2]/ul[3]/li/a"))).click();
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Add movement charges", isPass, timeTakenString);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Add Charges", isPass, "N/A", timeTakenString));
		}
	}
	
	public void close() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
	        driver.get(movementUrl);
	        Thread.sleep(6000);
	        // Search Movement by Consolidation Number
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number_text"))).sendKeys(consolidationNum);
	        WebElement element17 = driver.findElement(By.id("consolidation_number_text"));
	        element17.sendKeys(Keys.TAB);
	        Thread.sleep(6000);
	       
	        // Navigate to Close movement UI
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("topLinks"))).click();
	        driver.findElement(By.className("close")).click();
	        Thread.sleep(5000);
	        driver.findElement(By.id("closeDialogForm")).click();
	       
	        //driver.findElement(By.className("required")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("close_status_local_datetime_close"))).sendKeys(timeStamp.getCloseDate());
	        driver.findElement(By.id("time")).sendKeys(timeStamp.getCloseTime());
	        driver.findElement(By.id("close_remark_text")).sendKeys("TESTING");
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("uti-close-movement-close-movement"))).click();
	        long timeBeforeSave = System.currentTimeMillis();
	        // Wait until Movement status = "CLOSED"
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Movement status updated successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
//	        Thread.sleep(7000);
	        movementStatus= driver.findElement(By.id("movement_status")).getText();
	        
	        if (debugMode) {
	        	System.out.println("Movement status = "+ movementStatus);
	        }
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally{
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			String testValue= "Movement status = "+ movementStatus;
			if (!movementStatus.equalsIgnoreCase("CLOSED")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: CLOSED";
        	}
		
    	else{
    		isPass = "PASS";
    	}
    	
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Close movement", isPass, timeTakenString, testValue);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print EXCEL report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Close Movement", isPass, testValue, timeTakenString));
	        
		}
		
	}
		
	public void tender() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
	        driver.get(movementUrl);
	        Thread.sleep(6000);
	        // Search Movement by Consolidation Number
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number_text"))).sendKeys(consolidationNum);
	        WebElement element17 = driver.findElement(By.id("consolidation_number_text"));
	        element17.sendKeys(Keys.TAB);
	        Thread.sleep(6000);
	       
	        // Navigate to Close movement UI
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("topLinks"))).click();
	        driver.findElement(By.className("tender")).click();
	        Thread.sleep(5000);
	        driver.findElement(By.id("tenderDialogForm")).click();
	       
	        //driver.findElement(By.className("required")).click();
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("tender_status_local_datetime"))).sendKeys(timeStamp.getCloseDate());
	        driver.findElement(By.id("time")).sendKeys(timeStamp.getCloseTime());
	        driver.findElement(By.id("tender_remark_text")).sendKeys("TESTING");
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("uti-tender-movement-tender-movement"))).click();
	        long timeBeforeSave = System.currentTimeMillis();
	        // Wait until Movement status = "TENDERED"
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Movement status updated successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
//	        Thread.sleep(7000);
	        movementStatus= driver.findElement(By.id("movement_status")).getText();
	        
	        if (debugMode) {
	        	System.out.println("MovementStatus = "+ movementStatus);
	        }
	        String testValue= "MovementStatus = "+ movementStatus;
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			String testValue= "Movement status = "+ movementStatus;
			if (!movementStatus.equalsIgnoreCase("TENDERED")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: TENDERED";
        	}
			else{
				isPass="PASS";
			}
			
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Tender movement", isPass, timeTakenString, testValue);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Tender Movement", isPass, testValue, timeTakenString));
	        
		}
	}
	
	public void recoverContainer() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
	        driver.get(movementUrl);
	        Thread.sleep(4000);
	        // Search Movement by Consolidation Number
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number_text"))).sendKeys(consolidationNum);
	        WebElement element19 = driver.findElement(By.id("consolidation_number_text"));
	        element19.sendKeys(Keys.TAB);
	        Thread.sleep(6000);
	        // Navigate to Close movement UI
	        driver.findElement(By.id("topLinks")).click();
	        driver.findElement(By.className("recover")).click();
	        Thread.sleep(4000);
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("movement_recovery_form"))).click();
	        driver.findElement(By.id("freight_status_status_local_date")).sendKeys(timeStamp.getRecoverDate());
	        driver.findElement(By.id("freight_status_local_time")).sendKeys(timeStamp.getRecoverTime());
	        driver.findElement(By.id("jqgh_movement_recovery_container_grid_recovered")).click();
	        driver.findElement(By.id("recover_all_data")).click();
	        driver.findElement(By.id("recover_movement")).click();
	        
	        long timeBeforeSave = System.currentTimeMillis();
	        // Wait until Movement status = "RECOVERED"
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Recovered successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
	        
	        driver.findElement(By.id("hide_recover_movement")).click();
	        Thread.sleep(5000);
	        movementStatus= driver.findElement(By.id("movement_status")).getText();
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			String testValue= "Movement status = "+ movementStatus;
			if (!movementStatus.equalsIgnoreCase("RECOVERED")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: RECOVERED";
        	}
			else{
				isPass="PASS";
						
			}
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Recover movement", isPass, timeTakenString, testValue);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Recover Movement", isPass, testValue, timeTakenString));
	        
		}
	}
	
	public void recoverPieces() {
		String isPass;
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
	        driver.get(movementUrl);
	        Thread.sleep(4000);
	        // Search Movement by Consolidation Number
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number_text"))).sendKeys(consolidationNum);
	        WebElement element19 = driver.findElement(By.id("consolidation_number_text"));
	        element19.sendKeys(Keys.TAB);
	        Thread.sleep(6000);
	        // Navigate to Close movement UI
	        driver.findElement(By.id("topLinks")).click();
	        driver.findElement(By.className("recover")).click();
	        Thread.sleep(4000);
	        wait.until(ExpectedConditions.presenceOfElementLocated(By.id("movement_recovery_form"))).click();
	        driver.findElement(By.id("freight_status_status_local_date")).sendKeys(timeStamp.getRecoverDate());
	        driver.findElement(By.id("freight_status_local_time")).sendKeys(timeStamp.getRecoverTime());
	        driver.findElement(By.className("pieces_recovered valid")).sendKeys(movementData.getRecoverPiece());
	        //driver.findElement(By.id("recover_all_data")).click();
	        driver.findElement(By.id("recover_movement")).click();
	        long timeBeforeSave = System.currentTimeMillis();
	        // Wait until Movement status = "RECOVERED"
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Recovered successfully");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;

	        driver.findElement(By.id("hide_recover_movement")).click();
	        Thread.sleep(5000);
	        String MovementStatus= driver.findElement(By.id("movement_status")).getText();
	        String testValue= "MovementStatus = "+ MovementStatus;
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			
			String testValue= "Movement status = "+ movementStatus;
			if (!movementStatus.equalsIgnoreCase("RECOVERED")) {
        		isPass = "FAIL";
        		testValue += "\nExpected movement status: RECOVERED";
        	}
			else{
				isPass = "PASS";
			}
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Recover movement", isPass, timeTakenString, testValue);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "Recover Movement", isPass, testValue, timeTakenString));
		}   
	}
	
	// To-do: add time taken
	public void assignShipment(String shipmentNumber) {
		String isPass ="PASS";
		double testTimeTakenSec = 0;
		
		try {
			//Navigate to Movement UI
			driver.get(movementUrl);
			Thread.sleep(6000);
			// Search Movement by Consolidation Number
			wait.until(ExpectedConditions.presenceOfElementLocated(By.id("consolidation_number_text"))).sendKeys(consolidationNum);
			WebElement element17 = driver.findElement(By.id("consolidation_number_text"));
			element17.sendKeys(Keys.TAB);
			Thread.sleep(5000);
			// Navigate to EasyShipment on Movement UI
			wait.until(ExpectedConditions.presenceOfElementLocated(By.className("easy_form"))).click();
			wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div[4]/div/div/div/div/div[2]/div/div/div/form/ul/li"))).click();
			wait.until(ExpectedConditions.presenceOfElementLocated(By.id("shipment_number"))).sendKeys(shipmentNumber);
			driver.findElement(By.id("assignShipment")).click();
			long timeBeforeSave = System.currentTimeMillis();
	        wait.until(new ExpectedCondition<Boolean>(){
    	        @Override
    	        public Boolean apply(WebDriver d) {
    	        	return d.findElement(By.className("errString")).getText().equalsIgnoreCase("Shipment(s) successfully assigned to movement");
    	    	}
	        });
	        long timeAfterSave = System.currentTimeMillis();
	        testTimeTakenSec = (timeAfterSave - timeBeforeSave) / 1000.0;
		}
		catch (Exception e) {
        	e.printStackTrace();
        	if (e instanceof InterruptedException) {
        		// Currently ignore exception thrown by "Thread.sleep"
        	}
        	else {
        		// Other exceptions such as certain element not found considered as fail.
        		isPass = "FAIL";
        	}
        	        }
		finally {
			String timeTakenString = null;
			if (testTimeTakenSec < epsilon) {
				timeTakenString = "-";
			}
			else {
				timeTakenString = String.valueOf(testTimeTakenSec);
			}
			try {
				//Print EXCEL report
				resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Freight", "Assign shipment to movement2", isPass, timeTakenString);
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			//Print HTML report
		    AirSmokeTest.RESULT_ARRAY.add(new Results("Freight", "AssignShipmentToMovement2", isPass, "Freight Assignment", timeTakenString));
			
		}
	}
	
	public void verifyMovementPo() throws InterruptedException, IOException{
		String isPass="PASS";
		
		Thread.sleep(3000);
		wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div[2]/ul[3]/li/a"))).click();
		
		Thread.sleep(5000);
		WebElement table = driver.findElement(By.id("billing_charges_grid"));
				
		table.findElement(By.xpath("/html/body/div[4]/div/div/div[3]/div[2]/div/div[3]/div[3]/div/table/tbody/tr[2]")).click();
		
		driver.findElement(By.id("chargeDetail")).click();
		
		mvmtChargeDetails();
		
		System.out.println("PO time stamp" + potimestamp);
		
		String testValue= "Movement PO number  = "+ getCarrierMasterBillNum();
		if ( potimestamp !=null )
		{ 
			isPass = "PASS";
		
		}
		else if ( potimestamp == " " )
		{
			isPass = "FAIL";
		}
		//Print EXCEL report
		resultSheet.writeTestStepResult(++AirSmokeTest.TESTSTEP, "Movement", "Verify Movement PO", isPass, 0, testValue);
		//Print HTML report 
		AirSmokeTest.RESULT_ARRAY.add(new Results("PO", "VerifyPO", isPass, "N/A", "N/A"));
		
	}
	
	public void mvmtChargeDetails()
	{
		String isPass = "PASS";
	    boolean a = true;
		try {
	        Alert alert = driver.switchTo().alert();
	        String AlertText = alert.getText();
	        System.out.println(AlertText);
	        alert.accept();
	        a  = true;
	    } catch (Exception e) {
	        a = false;
	    }finally {
	        if (a != true) {
	            // take ur screenshot or whatever
	           potimestamp=  driver.findElement(By.id("initial_po_sent_timestamp")).getText();
	         //Print HTML report
	           AirSmokeTest.RESULT_ARRAY.add(new Results("Movement", "MovementChargesDetails", isPass, "N/A", "N/A"));
	        }
	    }

	}
	
	public String getConsolidationNumber() {
		return this.consolidationNum;
	}
	
	public String getCarrierMasterBillNum() {
		return this.carrierMasterBillNum;
	}
	
	public String getMovementStatus() {
		return this.movementStatus;
	}
}

